#!/bin/bash

SELF="djeploy"
APP_ROOT="/home/webserveruser"
DJANGO_APPS_DIR="$APP_ROOT/apps"
SVN_SERVER="https://svn.mycompany.com/svn"


PROJNAME="$2"
PROJDIR="$DJANGO_APPS_DIR/$PROJNAME"
PIDFILE="$PROJDIR/fastcgi.pid"
SOCKET="$PROJDIR/fastcgi.sock"
DEFAULT_START_ARGS="method=threaded umask=121 socket=$SOCKET pidfile=$PIDFILE"
DEFAULT_PYTHONPATH="$PROJDIR/project-packages/:$PYTHONPATH"
if [ -z $1 ]
then
  echo ""
  echo "$SELF Usage:"
  echo ""
  echo "    $SELF action project_name"
  echo ""
  echo "    Where action is one of:"
  echo "        start        Start the fastcgi application"
  echo "                       found in the apps/project_name directory"
  echo ""
  echo "        stop         Stop the previously started fastcgi application."
  echo ""
  echo "        restart      Same as \"$SELF stop\" followed by \"$SELF start\""
  echo ""
  echo "        update       Run an \"svn up\" command on the project"
  echo "                       directory and then restart the fastcgi"
  echo "                       application"
  echo ""
  echo "        checkout     If apps/project_name directory doesn't"
  echo "                       exist, $SELF will attempt to do a Subversion"
  echo "                       checkout of the project from the default"
  echo "                       subversion server.  The default subversion"
  echo "                       server is hardcoded in the $SELF script."
  echo ""
  echo "        manage       Runs the project's manage.py with all the"
  echo "                       arguments passed along unmolested."
  echo ""
  echo "        list vhosts  Prints out a list of all lighty-vhost.conf"
  echo "                       files in any applications subdirectory."
  echo "                       The list printed is suitable for inclusion"
  echo "                       in the main lighty conf file."
  exit 1
fi


case $1 in
    "start")
        cd $PROJDIR
        echo "Starting app $PROJNAME"
        $0 manage $PROJNAME runfcgi $DEFAULT_START_ARGS;;

    "stop")
        cd $PROJDIR
        echo "Stopping app $PROJNAME"
        if [ -f $PIDFILE ]
        then
          kill `cat -- $PIDFILE`
          rm -f -- $PIDFILE
        fi;;

    "restart")
        $0 stop $PROJNAME
        $0 start $PROJNAME;;  

    "update")
        echo "Updating app $PROJNAME"
        svn up $PROJDIR
        $0 restart $PROJNAME;;

    "manage")
        cd $PROJDIR
        shift
        shift
        exec /usr/bin/env - PYTHONPATH=$DEFAULT_PYTHONPATH python manage.py $@;;

    "checkout")
        if [ -d $PROJDIR ]
        then
          echo "ERROR: CAN'T CHECKOUT TO AN EXISTING DIRECTORY"
          exit 1
        fi
        svn co $SVN_SERVER/$PROJNAME/ apps/$PROJNAME;;

    "list")
        if [ $PROJDIR="vhosts" ]
        then
          for dir in `ls apps`
          do
            if [ -d $DJANGO_APPS_DIR/$dir ]
            then
              VH_FILE=$DJANGO_APPS_DIR/$dir/lighty-vhost.conf
              if [ -e $VH_FILE ] 
              then
                echo "include \"$VH_FILE\""
              fi
            fi
          done
        fi;;

    *)
        $0
        echo ""
        echo ""
        echo "ERROR: INVALID ACTION SPECIFIED"
        exit 1;;
esac
